package cn.tedu.antconlonysever.contract.dao.persist.repository.impl;

import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.util.PageInfoToPageDataConverter;
import cn.tedu.antconlonysever.contract.dao.persist.mapper.ContractMapper;
import cn.tedu.antconlonysever.contract.dao.persist.repository.IContractRepository;
import cn.tedu.antconlonysever.contract.pojo.entity.Contract;
import cn.tedu.antconlonysever.contract.pojo.vo.ContractListVo;
import cn.tedu.antconlonysever.contract.pojo.vo.ContractStandardVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;
@Slf4j
@Repository
public class ContractRepositoryImpl implements IContractRepository {
    @Autowired
    private ContractMapper contractMapper;
    @Override
    public int insert(Contract contract) {
        log.debug("开始执行【插入合同】的数据访问，参数：{}", contract);
        return contractMapper.insert(contract);
    }

    @Override
    public int insertBatch(List<Contract> contractsList) {
        log.debug("开始执行【批量插入合同】的数据访问，参数：{}", contractsList);
        return contractMapper.insertBatch(contractsList);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【删除合同】的数据访问，参数：{}", id);
        return contractMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(Collection<Long> idList) {
        log.debug("开始执行【批量删除合同】的数据访问，参数：{}", idList);
        return contractMapper.deleteBatchIds(idList);
    }

    @Override
    public int update(Contract contract) {
        return contractMapper.updateById(contract);
    }

    @Override
    public int count() {
        log.debug("开始执行【统计合同的数量】的数据访问，参数：无");
        return contractMapper.selectCount(null);
    }

    @Override
    public int countTitle(String title) {
        log.debug("开始执行【统计匹配名称的合同的数量】的数据访问，参数：{}", title);
        QueryWrapper<Contract> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("title",title);
        return contractMapper.selectCount(queryWrapper);
    }

    @Override
    public int countOrderId(Long orderId) {
        QueryWrapper<Contract> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId);
        return contractMapper.selectCount(queryWrapper);
    }

    @Override
    public int countNameAndNotTitle(Long id,String title) {
        log.debug("开始执行【统计匹配ID但不匹配名称的相册的数量】的数据访问，ID：{}，名称：{}", id, title);
        QueryWrapper<Contract> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("title",title)
                .ne("id",id);
        return contractMapper.selectCount(queryWrapper);
    }

    @Override
    public ContractStandardVO getStandardById(Long id) {
        log.debug("开始执行【根据ID查询合同信息】的数据访问，参数：{}", id);
        return contractMapper.getStandardById(id);
    }

    @Override
    public ContractStandardVO getStandardCusName(String cusName) {
        return contractMapper.getStandardCusName(cusName);
    }

    @Override
    public PageData<ContractListVo> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询合同列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<ContractListVo> list = contractMapper.list();
        PageInfo<ContractListVo> pageInfo = new PageInfo<>(list);
        PageData<ContractListVo> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
